package com.example.zyfypt525lmj.Activity.zyfypt525lmj;

import android.content.SharedPreferences;

import java.util.List;

import cst.czh.a923.zyfypt_923czh.Bean.ArticleBean;
import cst.czh.a923.zyfypt_923czh.Bean.LoginBean;
import cst.czh.a923.zyfypt_923czh.Bean.SVideoBean;
import cst.czh.a923.zyfypt_923czh.Bean.TwareBean;
import cst.czh.a923.zyfypt_923czh.Bean.VideoBean;
import cst.czh.a923.zyfypt_923czh.Listener.AmicoolListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by apple on 2017/5/8.
 */

public  class ResModel<T> implements Amicoolface {
    private Retrofit retrofit;
    private ResService service;//声明这个变量
private SharedPreferences sp;
    private static final  String BASE_UAL="http://amicool.neusoft.edu.cn/";
    private String SESSION_ID;
    //private static final String SESSION_ID="2ts2j910hv4m270hipde4a67d7";

    public ResModel(String type,String s_id) {
        if(type.equals("gson")){
            retrofit=new Retrofit.Builder().baseUrl(BASE_UAL).addConverterFactory(GsonConverterFactory.create()).build();//将gson转换为对象
        }
        else{
            retrofit=new Retrofit.Builder().baseUrl(BASE_UAL).addConverterFactory(ScalarsConverterFactory.create()).build();
        }
        service=retrofit.create(ResService.class);//使用retrofit将service实现
        SESSION_ID=s_id;
    }

    public ResModel(String type) {
        if(type.equals("gson")){
            retrofit=new Retrofit.Builder().baseUrl(BASE_UAL).addConverterFactory(GsonConverterFactory.create()).build();//将gson转换为对象
        }
        else{
            retrofit=new Retrofit.Builder().baseUrl(BASE_UAL).addConverterFactory(ScalarsConverterFactory.create()).build();
        }
        service=retrofit.create(ResService.class);//使用retrofit将service实现
        SESSION_ID="2ts2j910hv4m270hipde4a67d7";
    }

    @Override
    public void login(String u, String p, final AmicoolListener l) {
        Call<LoginBean> call= service.login(u,p);//调用service里面的login方法，返回一个Loginbean类型，赋值到call这个局部变量里面。
        call.enqueue(new Callback<LoginBean>() {//线程？
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {//如果成功的话，就会有一个返回值  response
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                l.onFail(t.toString());

            }
        });



    }

    @Override
    public void reg(String u, String p, String t, String roleid, String realname,String email,final AmicoolListener l) {
        Call<String> call=service.reg(u,p,t,roleid,realname,email);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());

            }
        });

    }




    @Override
    public void getList(String mod, int page,  final AmicoolListener l) {
       /* if(mod.equals("video"))
        {

                Call<List<VideoBean>> call=service.getVideoList(mod,page,"2ts2j910hv4m270hipde4a67d7"
                );
                call.enqueue(new Callback<List<VideoBean>>() {
                    @Override
                    public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                        l.onFail(t.toString());

                    }
                });

        }

    }

    @Override
    public void getVideoDetail(String mod, int id, String s, AmicoolListener l) {

    }


}*/
        switch (mod){
            case "article":
                Call<List<ArticleBean>> call_a=service.getArticleList(mod,page,SESSION_ID);
                call_a.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "tware":
                Call<List<TwareBean>> call_t=service.getTwareList(mod,page,SESSION_ID);
                call_t.enqueue(new Callback<List<TwareBean>>() {
                    @Override
                    public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "video":
                Call<List<VideoBean>> call=service.getVideoList(mod,page,SESSION_ID);
                call.enqueue(new Callback<List<VideoBean>>() {
                    @Override
                    public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                        l.onResponse(response.body());
                    }
                    @Override
                    public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "svideo":
                Call<List<SVideoBean>> call_sv=service.getSVideoList("video",page,SESSION_ID);
                call_sv.enqueue(new Callback<List<SVideoBean>>() {
                    @Override
                    public void onResponse(Call<List<SVideoBean>> call, Response<List<SVideoBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<SVideoBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "tcase":
                Call<List<TcaseBean>> call_tc=service.getTcaseList(mod,page,SESSION_ID);
                call_tc.enqueue(new Callback<List<TcaseBean>>() {
                    @Override
                    public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<TcaseBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;



        }}

    @Override
    public void getDetail(String mod, int id, String s, AmicoolListener l) {

    }
}
